home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / dtime / data.1 / dtsetDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-08  |  13.3 KB  |  446 lines

  1. // datentryDlg.cpp : implementation file
  2. //
  3.  
  4. ////////////////////////////////////// Includes ///////////////////////////////
  5. #include "stdafx.h"
  6. #include "resource.h"
  7. #include "dtsetDlg.h"
  8.  
  9.  
  10. ////////////////////////////////////// Macros /////////////////////////////////
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17.  
  18. ///////////////////////////////////// Implementation //////////////////////////
  19. BEGIN_MESSAGE_MAP(CDTSetDlg, CDialog)
  20.     //{{AFX_MSG_MAP(CDTSetDlg)
  21.     ON_WM_PAINT()
  22.     ON_WM_QUERYDRAGICON()
  23.     ON_EN_CHANGE(IDC_CDATE_FORMAT, OnChangeCDateFormat)
  24.     ON_EN_CHANGE(IDC_CLDATE_FORMAT, OnChangeCLDateFormat)
  25.     ON_EN_CHANGE(IDC_CLTIMEOFDAY_FORMAT, OnChangeCLTimeOfDayFormat)
  26.     ON_EN_CHANGE(IDC_CLTIMESPAN_FORMAT, OnChangeCLTimeSpanFormat)
  27.     //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29.  
  30.  
  31. CDTSetDlg::CDTSetDlg(CWnd* pParent /*=NULL*/)
  32.     : CDialog(CDTSetDlg::IDD, pParent)
  33. {
  34.     //{{AFX_DATA_INIT(CDTSetDlg)
  35.     //}}AFX_DATA_INIT
  36.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  37.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  38.   m_JulianEnd = CDate(1582, 10, 4);
  39.   m_GregorianBegin = CDate(1582, 10, 15);
  40.   m_BeginDayOfWeek = CDate::MONDAY;
  41. }
  42.  
  43. void CDTSetDlg::DoDataExchange(CDataExchange* pDX)
  44. {
  45.     CDialog::DoDataExchange(pDX);
  46.     //{{AFX_DATA_MAP(CDTSetDlg)
  47.     //}}AFX_DATA_MAP
  48.   DDX_CDateControl(pDX, IDC_JULIAN_DATE, m_ctrlJulianEnd, DT_ONLYVALID);
  49.   DDX_CDateControl(pDX, IDC_GREGORIAN_DATE, m_ctrlGregorianBegin, DT_ONLYVALID);
  50.   DDX_CDate(pDX, m_ctrlJulianEnd, m_JulianEnd);
  51.   DDX_CDate(pDX, m_ctrlGregorianBegin, m_GregorianBegin);
  52.   DDV_GreaterThanCDate(pDX, m_GregorianBegin, m_JulianEnd);
  53.  
  54.   DDX_Text(pDX, IDC_CDATE_FORMAT, m_sCDateFormat);
  55.   DDX_Text(pDX, IDC_CLDATE_FORMAT, m_sCLDateFormat);
  56.   DDX_Text(pDX, IDC_CLTIMESPAN_FORMAT, m_sCLTimeSpanFormat);
  57.   DDX_Text(pDX, IDC_CLTIMEOFDAY_FORMAT, m_sCLTimeOfDayFormat);
  58.  
  59.  
  60.   if (pDX->m_bSaveAndValidate)
  61.   {
  62.     DDX_CBIndex(pDX, IDC_BEGINDAYOFWEEK, m_BeginDayOfWeek);
  63.     m_BeginDayOfWeek++;
  64.   }
  65.   else
  66.   {
  67.     int nIndex = m_BeginDayOfWeek-1;
  68.     DDX_CBIndex(pDX, IDC_BEGINDAYOFWEEK, nIndex);
  69.   }
  70. }
  71.  
  72. BOOL CDTSetDlg::OnInitDialog()
  73. {
  74.   //Add Day of Week strings to combo box
  75.   CComboBox* pDayOfWeekCtrl = (CComboBox*) GetDlgItem(IDC_BEGINDAYOFWEEK);
  76.   int nIndex;
  77.   for (WORD i = CDate::SUNDAY; i<=CDate::SATURDAY; i++)
  78.     nIndex = pDayOfWeekCtrl->InsertString(i - CDate::SUNDAY, CDate::GetFullStringDayOfWeek(i));
  79.  
  80.     CDialog::OnInitDialog();
  81.  
  82.     // Set the icon for this dialog.
  83.     SetIcon(m_hIcon, TRUE);            // Set big icon
  84.     SetIcon(m_hIcon, FALSE);        // Set small icon
  85.  
  86.   //refresh the example statics
  87.   OnChangeCDateFormat();
  88.   OnChangeCLDateFormat();
  89.   OnChangeCLTimeOfDayFormat();
  90.   OnChangeCLTimeSpanFormat();
  91.  
  92.     return TRUE;
  93. }
  94.  
  95. // If you add a minimize button to your dialog, you will need the code below
  96. //  to draw the icon.  For MFC applications using the document/view model,
  97. //  this is automatically done for you by the framework.
  98. void CDTSetDlg::OnPaint() 
  99. {
  100.     if (IsIconic())
  101.     {
  102.         CPaintDC dc(this); // device context for painting
  103.  
  104.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  105.  
  106.         // Center icon in client rectangle
  107.         int cxIcon = GetSystemMetrics(SM_CXICON);
  108.         int cyIcon = GetSystemMetrics(SM_CYICON);
  109.         CRect rect;
  110.         GetClientRect(&rect);
  111.         int x = (rect.Width() - cxIcon + 1) / 2;
  112.         int y = (rect.Height() - cyIcon + 1) / 2;
  113.  
  114.         // Draw the icon
  115.         dc.DrawIcon(x, y, m_hIcon);
  116.     }
  117.     else
  118.     {
  119.         CDialog::OnPaint();
  120.     }
  121. }
  122.  
  123. // The system calls this to obtain the cursor to display while the user drags
  124. //  the minimized window.
  125. HCURSOR CDTSetDlg::OnQueryDragIcon()
  126. {
  127.     return (HCURSOR) m_hIcon;
  128. }
  129.  
  130. void CDTSetDlg::OnChangeCDateFormat() 
  131. {
  132.     CString sText;
  133.   GetDlgItemText(IDC_CDATE_FORMAT, sText);
  134.   CString& sEg = (CDate::CurrentDate()).Format(sText);
  135.   SetDlgItemText(IDC_CDATE_EG, sEg);
  136. }
  137.  
  138. void CDTSetDlg::OnChangeCLDateFormat() 
  139. {
  140.     CString sText;
  141.   GetDlgItemText(IDC_CLDATE_FORMAT, sText);
  142.   CString& sEg = (CLDate::CurrentTime(LOCAL)).Format(sText);
  143.   SetDlgItemText(IDC_CLDATE_EG, sEg);
  144. }
  145.  
  146. void CDTSetDlg::OnChangeCLTimeOfDayFormat() 
  147. {
  148.     CString sText;
  149.   GetDlgItemText(IDC_CLTIMEOFDAY_FORMAT, sText);
  150.   CString& sEg = (CLTimeOfDay::CurrentTimeOfDay(LOCAL)).Format(sText);
  151.   SetDlgItemText(IDC_CLTIMEOFDAY_EG, sEg);
  152. }
  153.  
  154. void CDTSetDlg::OnChangeCLTimeSpanFormat() 
  155. {
  156.     CString sText;
  157.   GetDlgItemText(IDC_CLTIMESPAN_FORMAT, sText);
  158.   CString& sEg = (CLTimeSpan(1, 2, 3, 4, 5)).Format(sText);
  159.   SetDlgItemText(IDC_CLTIMESPAN_EG, sEg);
  160. }
  161.  
  162.  
  163. void CDTSetDlg::LoadSettings()
  164. {
  165.   // Read the current state from the registry
  166.   // Try opening the registry key:
  167.   // HKEY_CURRENT_USER\Control Panel\DTime
  168.   HKEY hcpl;
  169.   if (RegOpenKeyEx(HKEY_CURRENT_USER,
  170.                    _T("Control Panel"),
  171.                    0,
  172.                    KEY_QUERY_VALUE,
  173.                    &hcpl) == ERROR_SUCCESS) 
  174.   {
  175.     HKEY happ;
  176.     if (RegOpenKeyEx(hcpl,
  177.                      _T("DTime"),
  178.                      0,
  179.                      KEY_QUERY_VALUE,
  180.                      &happ) == ERROR_SUCCESS) 
  181.     {
  182.       // Yes we are installed
  183.  
  184.       //First the begining day of week
  185.       DWORD dwType = 0;
  186.       DWORD dwSize = sizeof(m_BeginDayOfWeek);
  187.       RegQueryValueEx(happ,
  188.                       _T("BeginDayOfWeek"),
  189.                       NULL,
  190.                       &dwType,
  191.                       (BYTE*)&m_BeginDayOfWeek,
  192.                       &dwSize);
  193.  
  194.  
  195.       //the day part of the julian end date
  196.       dwType = 0;
  197.       DWORD Day;
  198.       dwSize = sizeof(DWORD);
  199.       RegQueryValueEx(happ,
  200.                       _T("JulianEndDay"),
  201.                       NULL,
  202.                       &dwType,
  203.                       (BYTE*)&Day,
  204.                       &dwSize);
  205.  
  206.       //the month part of the julian end date
  207.       dwType = 0;
  208.       DWORD Month;
  209.       dwSize = sizeof(DWORD);
  210.       RegQueryValueEx(happ,
  211.                       _T("JulianEndMonth"),
  212.                       NULL,
  213.                       &dwType,
  214.                       (BYTE*)&Month,
  215.                       &dwSize);
  216.  
  217.       //the year part of the julian end date
  218.       dwType = 0;
  219.       LONG Year;
  220.       dwSize = sizeof(LONG);
  221.       RegQueryValueEx(happ,
  222.                       _T("JulianEndYear"),
  223.                       NULL,
  224.                       &dwType,
  225.                       (BYTE*)&Year,
  226.                       &dwSize);
  227.  
  228.       m_JulianEnd = CDate(Year, (WORD) Month, (WORD) Day);
  229.  
  230.  
  231.  
  232.       //the day part of the gregorian begin date
  233.       dwType = 0;
  234.       dwSize = sizeof(DWORD);
  235.       RegQueryValueEx(happ,
  236.                       _T("GregorianBeginDay"),
  237.                       NULL,
  238.                       &dwType,
  239.                       (BYTE*)&Day,
  240.                       &dwSize);
  241.  
  242.       //the month part of the gregorian begin date
  243.       dwType = 0;
  244.       dwSize = sizeof(DWORD);
  245.       RegQueryValueEx(happ,
  246.                       _T("GregorianBeginMonth"),
  247.                       NULL,
  248.                       &dwType,
  249.                       (BYTE*)&Month,
  250.                       &dwSize);
  251.  
  252.       //the year part of the gregorian begin date
  253.       dwType = 0;
  254.       dwSize = sizeof(LONG);
  255.       RegQueryValueEx(happ,
  256.                       _T("GregorianBeginYear"),
  257.                       NULL,
  258.                       &dwType,
  259.                       (BYTE*)&Year,
  260.                       &dwSize);
  261.  
  262.       m_GregorianBegin = CDate(Year, (WORD) Month, (WORD) Day);
  263.  
  264.  
  265.  
  266.  
  267.       //the default CDate format string
  268.       dwSize = 1024;
  269.       LPTSTR pBuf = m_sCDateFormat.GetBufferSetLength(dwSize);
  270.       RegQueryValueEx(happ,
  271.                       _T("CDateDefaultFormat"),
  272.                       NULL,
  273.                       &dwType,
  274.                       (BYTE*)pBuf,
  275.                       &dwSize);
  276.       m_sCDateFormat.ReleaseBuffer();
  277.  
  278.       //the default CLDate format string
  279.       dwSize = 1024;
  280.       pBuf = m_sCLDateFormat.GetBufferSetLength(dwSize);
  281.       RegQueryValueEx(happ,
  282.                       _T("CLDateDefaultFormat"),
  283.                       NULL,
  284.                       &dwType,
  285.                       (BYTE*)pBuf,
  286.                       &dwSize);
  287.       m_sCLDateFormat.ReleaseBuffer();
  288.  
  289.       //the default CLTimeSpan format string
  290.       dwSize = 1024;
  291.       pBuf = m_sCLTimeSpanFormat.GetBufferSetLength(dwSize);
  292.       RegQueryValueEx(happ,
  293.                       _T("CLTimeSpanDefaultFormat"),
  294.                       NULL,
  295.                       &dwType,
  296.                       (BYTE*)pBuf,
  297.                       &dwSize);
  298.       m_sCLTimeSpanFormat.ReleaseBuffer();
  299.  
  300.       //the default CLTimeOfDay format string
  301.       dwSize = 1024;
  302.       pBuf = m_sCLTimeOfDayFormat.GetBufferSetLength(dwSize);
  303.       RegQueryValueEx(happ,
  304.                       _T("CLTimeOfDayDefaultFormat"),
  305.                       NULL,
  306.                       &dwType,
  307.                       (BYTE*)pBuf,
  308.                       &dwSize);
  309.       m_sCLTimeOfDayFormat.ReleaseBuffer();
  310.  
  311.       RegCloseKey(happ);
  312.     }
  313.     RegCloseKey(hcpl);
  314.   }
  315. }
  316.  
  317. void CDTSetDlg::SaveSettings()
  318. {
  319.   // Update the registry
  320.   // Try creating/opening the registry key
  321.   HKEY hcpl;
  322.  
  323.   if (RegOpenKeyEx(HKEY_CURRENT_USER,
  324.                    _T("Control Panel"),
  325.                    0,
  326.                    KEY_WRITE,
  327.                    &hcpl) == ERROR_SUCCESS) 
  328.   {
  329.     HKEY happ;
  330.     DWORD dwDisp;
  331.     if (RegCreateKeyEx(hcpl,
  332.                        _T("DTime"),
  333.                        0,
  334.                        _T(""),
  335.                        REG_OPTION_NON_VOLATILE,
  336.                        KEY_WRITE,
  337.                        NULL,
  338.                        &happ,
  339.                        &dwDisp) == ERROR_SUCCESS) 
  340.     {
  341.  
  342.       // Set the begining day of week
  343.       RegSetValueEx(happ,
  344.                     _T("BeginDayOfWeek"),
  345.                     0,
  346.                     REG_DWORD,
  347.                     (BYTE*)&m_BeginDayOfWeek,
  348.                     sizeof(m_BeginDayOfWeek));
  349.  
  350.       // Set the day part of the end of julian
  351.       DWORD Day = (DWORD) m_JulianEnd.GetDay();
  352.       RegSetValueEx(happ,
  353.                     _T("JulianEndDay"),
  354.                     0,
  355.                     REG_DWORD,
  356.                     (BYTE*)&Day,
  357.                     sizeof(DWORD));
  358.  
  359.       // Set the month part of the end of julian
  360.       DWORD Month = (DWORD) m_JulianEnd.GetMonth();
  361.       RegSetValueEx(happ,
  362.                     _T("JulianEndMonth"),
  363.                     0,
  364.                     REG_DWORD,
  365.                     (BYTE*)&Month,
  366.                     sizeof(DWORD));
  367.  
  368.       // Set the year part of the end of julian
  369.       LONG Year = m_JulianEnd.GetYear();
  370.       RegSetValueEx(happ,
  371.                     _T("JulianEndYear"),
  372.                     0,
  373.                     REG_DWORD,
  374.                     (BYTE*)&Year,
  375.                     sizeof(LONG));
  376.  
  377.       // Set the day part of the begin of gregorian
  378.       Day = (DWORD) m_GregorianBegin.GetDay();
  379.       RegSetValueEx(happ,
  380.                     _T("GregorianBeginDay"),
  381.                     0,
  382.                     REG_DWORD,
  383.                     (BYTE*)&Day,
  384.                     sizeof(DWORD));
  385.  
  386.       // Set the month part of the begin of gregorian
  387.       Month = (DWORD) m_GregorianBegin.GetMonth();
  388.       RegSetValueEx(happ,
  389.                     _T("GregorianBeginMonth"),
  390.                     0,
  391.                     REG_DWORD,
  392.                     (BYTE*)&Month,
  393.                     sizeof(DWORD));
  394.  
  395.       // Set the year part of the begin of gregorian
  396.       Year = m_GregorianBegin.GetYear();
  397.       RegSetValueEx(happ,
  398.                     _T("GregorianBeginYear"),
  399.                     0,
  400.                     REG_DWORD,
  401.                     (BYTE*)&Year,
  402.                     sizeof(LONG));
  403.  
  404.  
  405.       // Set the default CDate Format string
  406.       RegSetValueEx(happ,
  407.                     _T("CDateDefaultFormat"),
  408.                     0,
  409.                     REG_SZ,
  410.                     (BYTE*) m_sCDateFormat.GetBuffer(m_sCDateFormat.GetLength()),
  411.                     sizeof(TCHAR) * (m_sCDateFormat.GetLength() + 1));
  412.       m_sCDateFormat.ReleaseBuffer();
  413.  
  414.       // Set the default CLDate Format string
  415.       RegSetValueEx(happ,
  416.                     _T("CLDateDefaultFormat"),
  417.                     0,
  418.                     REG_SZ,
  419.                     (BYTE*) m_sCLDateFormat.GetBuffer(m_sCLDateFormat.GetLength()),
  420.                     sizeof(TCHAR) * (m_sCLDateFormat.GetLength() + 1));
  421.       m_sCLDateFormat.ReleaseBuffer();
  422.  
  423.       // Set the default CLTimeSpan Format string
  424.       RegSetValueEx(happ,
  425.                     _T("CLTimeSpanDefaultFormat"),
  426.                     0,
  427.                     REG_SZ,
  428.                     (BYTE*) m_sCLTimeSpanFormat.GetBuffer(m_sCLTimeSpanFormat.GetLength()),
  429.                     sizeof(TCHAR) * (m_sCLTimeSpanFormat.GetLength() + 1));
  430.       m_sCLTimeSpanFormat.ReleaseBuffer();
  431.  
  432.       // Set the default CLTimeOfDay Format string
  433.       RegSetValueEx(happ,
  434.                     _T("CLTimeOfDayDefaultFormat"),
  435.                     0,
  436.                     REG_SZ,
  437.                     (BYTE*) m_sCLTimeOfDayFormat.GetBuffer(m_sCLTimeOfDayFormat.GetLength()),
  438.                     sizeof(TCHAR) * (m_sCLTimeOfDayFormat.GetLength() + 1));
  439.       m_sCLTimeOfDayFormat.ReleaseBuffer();
  440.  
  441.       // Finished with keys
  442.       RegCloseKey(happ);
  443.     }
  444.     RegCloseKey(hcpl);
  445.   }
  446. }